Skip to content

[MCP] Bug-fix- aggregate_records always requires groupby#3294

Open
souvikghosh04 wants to merge 3 commits intomainfrom
Usr/sogh/aggregate_fix_groupby
Open

[MCP] Bug-fix- aggregate_records always requires groupby#3294
souvikghosh04 wants to merge 3 commits intomainfrom
Usr/sogh/aggregate_fix_groupby

Conversation

@souvikghosh04
Copy link
Contributor

@souvikghosh04 souvikghosh04 commented Mar 18, 2026

Why make this change?

The aggregate_records MCP tool always rejects requests without groupby because the orderby schema has "default": "desc", causing LLMs to always include it, which then triggers a validation error requiring groupby. Simple aggregations like SELECT COUNT(*) FROM table had to go through this kind of behaviour and complexity.

What is this change?

Removed "default": "desc" from the orderby input schema so LLMs no longer auto-populate it.
Updated ValidateGroupByDependencies to silently ignore orderby when groupby is absent instead of returning an error. Ordering is meaningless without grouping, so the parameter is harmlessly cleared via a ref flag.

How was this tested?

  • Integration Tests
  • Unit Tests
  • Manual Tests (VS code GHCP)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes aggregate_records MCP tool behavior so simple aggregations (e.g., count(*)) don’t get blocked by an auto-populated orderby that previously forced groupby, addressing issue #3279.

Changes:

  • Removed the orderby schema default so models/clients are less likely to send orderby unnecessarily.
  • Updated groupby dependency validation to ignore orderby when groupby is absent (instead of erroring).
  • Added/updated tests to cover the “orderby without groupby” scenario and to assert the schema no longer includes an orderby default.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/Azure.DataApiBuilder.Mcp/BuiltInTools/AggregateRecordsTool.cs Removes orderby default from tool schema and changes validation to clear/ignore orderby when groupby is not present.
src/Service.Tests/Mcp/AggregateRecordsToolTests.cs Replaces the previous “orderby without groupby” rejection test with new coverage asserting the new behavior and schema shape.

You can also share your feedback on Copilot code review. Take the survey.

[DataRow("{\"entity\": \"Book\", \"function\": \"count\", \"field\": \"*\", \"orderby\": \"asc\"}",
DisplayName = "count(*) with orderby asc, no groupby")]
[DataRow("{\"entity\": \"Book\", \"function\": \"avg\", \"field\": \"price\", \"orderby\": \"desc\"}",
DisplayName = "avg(price) with orderby desc, no groupby")]
Copy link
Collaborator

@Aniruddh25 Aniruddh25 Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to change the aggregation function - avg, sum to test the same input validation as done for count

Essentially, avg and sum tests are redundant, can be removed

{
bool userProvidedOrderby = true;
CallToolResult? result = AggregateRecordsTool.ValidateGroupByDependencies(
groupbyCount: 0,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests above regarding validation already cover this.

Copy link
Collaborator

@Aniruddh25 Aniruddh25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove some redundant tests

@github-project-automation github-project-automation bot moved this from In Progress to Review In Progress in Data API builder Mar 20, 2026
@Aniruddh25 Aniruddh25 self-assigned this Mar 20, 2026
@Aniruddh25
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.0 bug Something isn't working mcp-server

Projects

Status: Review In Progress

Development

Successfully merging this pull request may close these issues.

[Bug]: aggregate_records ALWAYS requires groupby

4 participants